package com.voicechat.firstmet.fragment;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.activity.InviteVoiceChatActivity;
import com.voicechat.firstmet.activity.PersonalHomepageActivity;
import com.voicechat.firstmet.adapter.VoiceChatListAdapter;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.TimeRecomBean;
import com.voicechat.firstmet.bean.VoiceChatAllBean;
import com.voicechat.firstmet.bean.VoiceChatBean;
import com.voicechat.firstmet.client.OssClient;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.client.WsManager;
import com.voicechat.firstmet.imcontrol.BroadcastManager;
import com.voicechat.firstmet.imcontrol.MediaUtil;
import com.voicechat.firstmet.imcontrol.RoomHelper;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.MeInfoService;
import com.voicechat.firstmet.net.service.VoiceChatService;
import com.voicechat.firstmet.utils.CheckAudioPermission;
import com.voicechat.firstmet.utils.ConstantSet;
import com.voicechat.firstmet.utils.FileUtils;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.ThreadManager;
import com.voicechat.firstmet.utils.Utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.fragment
 * @ClassName: VoiceChatListFragment
 * @Description: 语聊嵌套fragment
 * @Author: ljp
 * @CreateDate: 2018/9/6 9:28
 * @Version: 1.0
 */
public class VoiceChatListFragment extends BaseFragment {
    @BindView(R.id.recycler_voice_chat)
    RecyclerView recyclerVoiceChat;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    private String flag;
    private VoiceChatListAdapter chatListAdapter;
    private ArrayList<VoiceChatAllBean.PageBean.ListBean> voiceChatAllBeans;
    private ArrayList<TimeRecomBean.ListBean> listBeans;
    private String TAG = "VoiceChatListFragment";
    private int page;
    private Runnable runnable;
    private MediaUtil mediaUtil;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private RoomHelper helper;
    private VoiceChatBean voiceChatBean;
    private String otherId;


    public static VoiceChatListFragment newInstance(String flag) {
        VoiceChatListFragment voiceChatListFragment = new VoiceChatListFragment();
        Bundle bundle = new Bundle();
        bundle.putString("flag", flag);
        voiceChatListFragment.setArguments(bundle);
        return voiceChatListFragment;
    }

    public static VoiceChatListFragment newInstance() {
        VoiceChatListFragment fragment = new VoiceChatListFragment();
        return fragment;
    }

    @Override
    protected int loadLayout() {
        return R.layout.fragment_voice_chatlist;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        flag = getArguments().getString("flag");
        ButterKnife.bind(this, view);
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        BroadcastManager.getInstance(activity).addAction("hidden", new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                setOriginalImage();
            }
        });
        initView();
    }

    private void initView() {
        smartRefresh.autoRefresh();
        page = 1;
        voiceChatAllBeans = new ArrayList<>();
        listBeans = new ArrayList<>();
        //smartRefresh.autoRefresh();
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                judgeFlag();
                smartRefresh.finishLoadMore(10000);
            }
        });
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                judgeFlag();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header  样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        chatListAdapter = new VoiceChatListAdapter(context, voiceChatAllBeans, flag);
        recyclerVoiceChat.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerVoiceChat.setAdapter(chatListAdapter);
        chatListAdapter.setOnClick(new VoiceChatListAdapter.OnClick() {
            @Override
            public void setIconClick(VoiceChatAllBean.PageBean.ListBean listBean) {
                Log.i(TAG, "setIconClick: -----" + listBean.getId());
                Intent intent = new Intent(activity, PersonalHomepageActivity.class);
                //Intent intent = new Intent(activity, VoiceChatingActivity.class);
                intent.putExtra("anchorId", listBean.getId());
                activity.startActivity(intent);
            }

            @Override
            public void setVoiceClick(VoiceChatAllBean.PageBean.ListBean listBean, View view, int position) {
                String path = ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH + Utils.getVoiceName(listBean.getVoiceInfo());
                Log.i(TAG, "onBindViewHolder: -----" + path);
                if (FileUtils.isExists(path)) {
                    try {
                        player(path, (ImageView) view, position,R.drawable.violet_voice_play);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void setVoiceClick(TimeRecomBean.ListBean listBean, View view, int position) {
                if (!TextUtils.isEmpty(listBean.getVoiceInfo())) {
                    String path = ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(listBean.getVoiceInfo());
                    Log.i(TAG, "onBindViewHolder: -----"+path);
                    if(FileUtils.isExists(path) ) {
                            try {
                                player(path, (ImageView) view,position,R.drawable.red_voice_play);
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            }
                    }
                }
            }

            @Override
            public void setVoiceChatClick(VoiceChatAllBean.PageBean.ListBean listBean) {
                otherId = listBean.getId();
                if(Build.VERSION.SDK_INT < 23){
                    if(CheckAudioPermission.isHasPermission(context)){
                        VoiceChatJude(listBean.getId());
                    }else {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.RECORD_AUDIO}, 1);
                    }
                }else {
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.RECORD_AUDIO}, 1);
                    } else {
                        VoiceChatJude(listBean.getId());
                    }
                }
            }

            @Override
            public void setVoiceChatClickTime(TimeRecomBean.ListBean listBean) {
                otherId = listBean.getUserId();
                if(Build.VERSION.SDK_INT < 23){
                    if(CheckAudioPermission.isHasPermission(context)){
                        VoiceChatJude(listBean.getUserId());
                    }else {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.RECORD_AUDIO}, 1);
                    }
                }else {
                    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO)
                            != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions(activity, new String[]{
                                Manifest.permission.RECORD_AUDIO}, 1);
                    } else {
                        VoiceChatJude(listBean.getUserId());
                    }
                }
            }
        });
        judgeFlag();
    }

    /**
     * @return
     * @method
     * @description flag判断是请求哪个list
     * @date: 2018/10/19 17:04
     * @author: ljp
     */
    private void judgeFlag() {
        // Log.i(TAG, "judgeFlag: ------"+flag);
        switch (flag) {
            case "ALL"://全部
                //voiceChatAllBeans.clear();
                if (page == 1) {
                    setHttpTimeList();
                }
                setHttpAllList();
                break;
            case "POPU"://人气
                // voiceChatAllBeans.clear();
                setHttpHotList();
                break;
            case "NEW"://新人
                //voiceChatAllBeans.clear();
                setHttpNewList();
                break;
        }
    }

    /**
     * @return
     * @method
     * @description 时段推荐请求
     * @date: 2018/10/19 16:39
     * @author: ljp
     */
    private void setHttpTimeList() {
        VoiceChatService.voiceChatTime(SPUtils.getEditoString(SPUtils.SP_TOKEN),
                PhoneUtils.getDeviceInfo(context), mySeleInfo.getId()).subscribe(timeRecomBean -> {
            Log.i(TAG, "setHttpTimeList: ---------" + timeRecomBean.toString());
            if (timeRecomBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                if(timeRecomBean.getList() != null){
                    listBeans.addAll(timeRecomBean.getList());
                    chatListAdapter.setTimeList(listBeans);
                    smartRefresh.finishRefresh();
                    setThreadTime(timeRecomBean.getList());
                }else {
                    smartRefresh.finishRefresh();
                }
            } else if(timeRecomBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(timeRecomBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "setHttpTimeList: ------------"+throwable.toString());

        });
    }
    /**
     * @return
     * @method
     * @description ALL请求
     * @date: 2018/10/19 16:39
     * @author: ljp
     */
    private void setHttpAllList() {
        VoiceChatService.voiceChatAll(SPUtils.getEditoString(SPUtils.SP_TOKEN),
                PhoneUtils.getDeviceInfo(context), page + "", mySeleInfo.getId()).subscribe(voiceChatAllBean -> {
            Log.i(TAG, "setHttpAllList: ------------"+voiceChatAllBean.getCode());
           // Log.i(TAG, "setHttpAllList: ------------"+voiceChatAllBean.getPage().getList().size());
            if (voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = voiceChatAllBean.getPage().getCount() / voiceChatAllBean.getPage().getPageSize() +
                        (voiceChatAllBean.getPage().getCount() % voiceChatAllBean.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (i != 0 && page > i) {//判断是否是最后一页 {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        voiceChatAllBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if(voiceChatAllBean.getPage().getList() != null) {
                        voiceChatAllBeans.addAll(voiceChatAllBean.getPage().getList());
                        chatListAdapter.notifyDataSetChanged();
                        setThread(voiceChatAllBean.getPage().getList());
                    }
                    //chatListAdapter.setHasStableIds(true);
                    smartRefresh.finishLoadMore();

                }
            } else if(voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(voiceChatAllBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "setHttpAllList: -------------"+throwable.toString());

        });
    }
    /**
     * @return
     * @method
     * @description 人气请求
     * @date: 2018/10/19 16:39
     * @author: ljp
     */
    private void setHttpHotList() {
        VoiceChatService.voiceChatHitsList(SPUtils.getEditoString(SPUtils.SP_TOKEN),
                PhoneUtils.getDeviceInfo(context), page + "", mySeleInfo.getId()).subscribe(voiceChatAllBean -> {
            Log.i(TAG, "setHttpAllList: ---------" + voiceChatAllBean.toString());
            if (voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = voiceChatAllBean.getPage().getCount() / voiceChatAllBean.getPage().getPageSize() +
                        (voiceChatAllBean.getPage().getCount() % voiceChatAllBean.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                    //Log.i(TAG, "communityPostList: -------------postLists.size();--------" + postLists.size());
                } else {
                    if (page == 1) {
                        voiceChatAllBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if(voiceChatAllBean.getPage().getList() != null) {
                        voiceChatAllBeans.addAll(voiceChatAllBean.getPage().getList());
                        chatListAdapter.notifyDataSetChanged();
                        setThread(voiceChatAllBean.getPage().getList());
                    }
                    smartRefresh.finishLoadMore();

                }
            } else if(voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(voiceChatAllBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "setHttpAllList: ---------" + throwable.toString());
        });
    }

    /**
     * @return
     * @method
     * @description 新人请求
     * @date: 2018/10/19 16:39
     * @author: ljp
     */
    private void setHttpNewList() {
        VoiceChatService.voiceChatNewList(SPUtils.getEditoString(SPUtils.SP_TOKEN),
                PhoneUtils.getDeviceInfo(context), page + "", mySeleInfo.getId()).subscribe(voiceChatAllBean -> {
            Log.i(TAG, "setHttpAllList: ---------" + voiceChatAllBean.toString());
            if (voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = voiceChatAllBean.getPage().getCount() / voiceChatAllBean.getPage().getPageSize() +
                        (voiceChatAllBean.getPage().getCount() % voiceChatAllBean.getPage().getPageSize() == 0 ? 0 : 1);
                //int i = postBean.getPage().getCount() % postBean.getPage().getPageSize();
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                    //Log.i(TAG, "communityPostList: -------------postLists.size();--------" + postLists.size());
                } else {
                    if (page == 1) {
                        voiceChatAllBeans.clear();
                        smartRefresh.finishRefresh();
                    }
                    if(voiceChatAllBean.getPage().getList() != null) {
                        voiceChatAllBeans.addAll(voiceChatAllBean.getPage().getList());
                        chatListAdapter.notifyDataSetChanged();
                        setThread(voiceChatAllBean.getPage().getList());
                    }
                    smartRefresh.finishLoadMore();

                }
            } else if(voiceChatAllBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(voiceChatAllBean.getMsg());
            }
        }, throwable -> {

        });
    }

    int mFirstVoicePlayItem;//第一级列表的喇叭动画初始位置
    int mSecondVoicePlayItem;//第二级列表的喇叭动画初始位置
    //int position;//当前列表的位置
    AnimationDrawable animationDrawable;
    ImageView imageViewIcon;

    public AnimationDrawable getAnimationDrawable() {
        return animationDrawable;
    }

    /**
     * @return
     * @method
     * @description 播放语音
     * @date: 2018/9/28 18:57
     * @author: ljp
     */
    private void player(String path, ImageView imageView, int position,int resource) throws FileNotFoundException {
        imageViewIcon = imageView;
        if (TextUtils.isEmpty(path)) {
            return;
        }
        mSecondVoicePlayItem = -1;
        if (animationDrawable != null) {
            MediaUtil.getInstance().stop();
            animationDrawable.stop();
            animationDrawable.selectDrawable(2);
            animationDrawable = null;
            if (mFirstVoicePlayItem != position) {
                mFirstVoicePlayItem = position;
                imageView.setImageResource(resource);
                animationDrawable = (AnimationDrawable) imageView.getDrawable();
                animationDrawable.start();
                MediaUtil.getInstance().play(new FileInputStream(path));
            }
        } else {
            mFirstVoicePlayItem = position;
            imageView.setImageResource(resource);
            animationDrawable = (AnimationDrawable) imageView.getDrawable();
            animationDrawable.start();
            MediaUtil.getInstance().play(new FileInputStream(path));
        }
        MediaUtil.getInstance().getPlayer().setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.stop();
                if(animationDrawable != null) {
                    animationDrawable.stop();
                    animationDrawable.selectDrawable(2);
                    animationDrawable = null;
                }
            }
        });
    }
    /**
     * @return
     * @method
     * @description 启动线程池下载时段推荐语音介绍信息
     * @date: 2018/11/15 15:14
     * @author: ljp
     */
    private void setThreadTime(List<TimeRecomBean.ListBean> listBeans) {
        ThreadManager.getThreadPool().execute(runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < listBeans.size(); i++) {
                    String voiceInfo = listBeans.get(i).getVoiceInfo();
                    int finalI = i;
                    // Log.i(TAG, "run: ------"+voiceInfo);
                    // Log.i(TAG, "run: -----"+ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(voiceInfo));
                    //Log.i(TAG, "run: ----"+FileUtils.isExists( ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(voiceInfo)));
                    OssClient.getInstance().init(context).fileDownload(voiceInfo, new OssClient.OnDownloadFile() {
                        @Override
                        public void onDownFileSuccess(InputStream inputStream) {
                            String voiceName = Utils.getVoiceName(listBeans.get(finalI).getVoiceInfo());
                            //Log.i(TAG, "onDownFileSuccess: ---------"+voiceName);
                            String path = ConstantSet.VOICEINTR;
                            try {
                                OssConfig.writeToLocal(path, inputStream, voiceName);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onDownFileProgress(String progress) {
                            //Log.i(TAG, "onDownFileProgress: ----------"+progress);
                        }
                    });
                }
            }
        });
    }

    /**
     * @return
     * @method
     * @description 启动线程池下载语音介绍信息
     * @date: 2018/11/15 15:14
     * @author: ljp
     */
    private void setThread(List<VoiceChatAllBean.PageBean.ListBean> voiceChatAllBeans) {
        ThreadManager.getThreadPool().execute(runnable = new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < voiceChatAllBeans.size(); i++) {
                    String voiceInfo = voiceChatAllBeans.get(i).getVoiceInfo();
                    int finalI = i;
                    // Log.i(TAG, "run: ------"+voiceInfo);
                    //Log.i(TAG, "run: -----"+ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(voiceInfo));
                    //Log.i(TAG, "run: ----"+FileUtils.isExists( ConstantSet.VOICEINTR + ConstantSet.VOICEINTR_PATH+ Utils.getVoiceName(voiceInfo)));
                    OssClient.getInstance().init(context).fileDownload(voiceInfo, new OssClient.OnDownloadFile() {
                        @Override
                        public void onDownFileSuccess(InputStream inputStream) {
                            String voiceName = Utils.getVoiceName(voiceChatAllBeans.get(finalI).getVoiceInfo());
                            //Log.i(TAG, "onDownFileSuccess: ---------"+voiceName);
                            String path = ConstantSet.VOICEINTR;
                            try {
                                OssConfig.writeToLocal(path, inputStream, voiceName);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onDownFileProgress(String progress) {
                            //Log.i(TAG, "onDownFileProgress: ----------"+progress);
                        }
                    });
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ThreadManager.getThreadPool().cancel(runnable);
        setOriginalImage();
    }
    @Override
    public void onStop() {
        super.onStop();
        ThreadManager.getThreadPool().cancel(runnable);
        setOriginalImage();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ----");
    }

    @Override
    public void onResume() {
        super.onResume();
        smartRefresh.autoRefresh();
        page = 1;
    }

    @Override
    public void onPause() {
        super.onPause();
        setOriginalImage();
        Log.i(TAG, "onPause: ---------");
    }

    /**
     * @return
     * @method
     * @description 向对方发起语音通话请求
     * @date: 2018/11/21 12:06
     * @author: ljp
     */
    private void VoiceChatJude(String userId) {
        MeInfoService.voiceChatJude(userId,SPUtils.getEditoString(SPUtils.SP_TOKEN), PhoneUtils.getDeviceInfo(context), mySeleInfo.getId()).subscribe(voiceChatBean -> {
            Log.i(TAG, "VoiceChatJude: ---------" + voiceChatBean.getCode());
            Log.i(TAG, "VoiceChatJude: ---------" + voiceChatBean.getMsg());
            //Log.i(TAG, "VoiceChatJude: ---------"+ voiceChatBean.getObject().toString());
            if(voiceChatBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_LINE){//自己下线，重连长链接
                WsManager.getInstance(context).reconnect(mySeleInfo.getId());
                toast_MakeText("请重试！");
            }else if (voiceChatBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_FAIL) {
                toast_MakeText(voiceChatBean.getMsg());
            } else if (voiceChatBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {//用户在线，可以通话
                this.voiceChatBean = voiceChatBean;
                Intent intent = new Intent(activity, InviteVoiceChatActivity.class);
                Bundle bundle = new Bundle();
                /*bundle.putString("type", "1");
                bundle.putString("flag", "0");*/
                bundle.putInt("roomId", voiceChatBean.getObject().getRoomId());
                bundle.putParcelable("voiceChat", voiceChatBean.getMap().getCalledUser());
                bundle.putParcelable("voiceChatCalling", voiceChatBean.getMap().getCallingUser());
                intent.putExtra("type","1");
                intent.putExtra("flag","0");
                intent.putExtra("bundle", bundle);
                activity.startActivity(intent);
            }else if(voiceChatBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            }else {
                toast_MakeText(voiceChatBean.getMsg());
            }
        }, throwable -> {
            toast_MakeText(getResources().getString(R.string.net_error));
            Log.i(TAG, "VoiceChatJude: ----------" + throwable.toString());
        });
    }
    /**
     * ⑨重写onRequestPermissionsResult方法
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
             VoiceChatJude(otherId);
        } else {
            Toast.makeText(activity, "用户拒绝了权限,请先去设置中打开录音权限", Toast.LENGTH_SHORT).show();
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(hidden){
            setOriginalImage();
        }
    }
    //设置默认背景图片
    private void setOriginalImage(){
        if (animationDrawable != null) {
            MediaUtil.getInstance().getPlayer().stop();
            animationDrawable.stop();
            animationDrawable.selectDrawable(2);
            animationDrawable = null;
            imageViewIcon.setImageResource(R.mipmap.violet_voice_chat_3);
        }
    }
}
